﻿using CommunityToolkit.Mvvm.ComponentModel;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.IO;
using TotalApplication.AngelSix;
using TotalApplication.Core;
using TotalApplication.Mine_Video;
using TotalApplication.services;

namespace TotalApplication.viewmodels.Video.Controls
{
    public partial class TagLIstVM : BaseViewModel
    {
        #region Properties

        [ObservableProperty]
        private ObservableCollection<TagItemVM> _tags = new ObservableCollection<TagItemVM>();


        [ObservableProperty]
        private double _width = 400;

        [ObservableProperty]
        private double _height = 400;

        /// <summary>
        /// 0——正常
        /// 1——编辑
        /// </summary>
        public int State = 0;

        /// <summary>
        /// 有无标签在展开状态
        /// </summary>
        public bool IsExpanded = false;

        public int ExpandedIndex = -1;

        public int TopIndex = 0;

        public TagItemVM? Parent = null;
        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public TagLIstVM()
        {

        }

        #endregion

        #region Commands

        public void Delete(TagItemVM tagItem)
        {
            if (tagItem.IsTop)
            {
                IoC.UI.ShowMessageBoxAsync(new MessageBoxDialogViewModel() { Message = "置顶标签无法删除，请取消置顶后再删除！", Title = "提示", OkText = "我知道了" });
                return;
            }
            if (Parent != null && Tags.Count == 1)
            {
                Parent.HasChild = false;
            }
            Tags.Remove(tagItem);
        }

        public void Add()
        {
            //if (IsExpanded)
            //{
            //    Tags[ExpandedIndex].Childs.Tags.Add(new TagItemVM() { Content = "", IsEdit = State == 1, IsChild = true ,Parent = Tags[ExpandedIndex].Childs });
            //    return;
            //}
            //Tags.Add(new TagItemVM() { Content = "", IsEdit = State == 1, Parent = this });

            if (Parent != null && Tags.Count == 0)
            {
                Parent.HasChild = true;
            }
            Tags.Add(new TagItemVM() { Content = "", IsEdit = State == 1 });
        }

        public void Edit()
        {
            if (State == 0)
            {
                //if (Tags.Count > 0 && Tags[0].IsEdit == true)
                //    return;
                State = 1;
                foreach (var item in Tags)
                {
                    item.IsEdit = true;
                    item.EditContent = item.Content;
                }
            }
            else
            {
                //if (Tags.Count > 0 && Tags[0].IsEdit == false)
                //    return;
                State = 0;
                foreach (var item in Tags)
                {
                    item.IsEdit = false;
                    item.Content = item.EditContent;
                }
            }
        }

        /// <summary>
        /// 置顶指定项方法【加精华】
        /// </summary>
        /// <param name="tagItem"></param>
        public void Top(TagItemVM tagItem)
        {
            if (tagItem.IsTop)
            {
                // 已经是置顶状态，取消置顶
                tagItem.IsTop = false;

                TopIndex -= 1;

                Tags.MoveToIndex(tagItem, TopIndex);
                return;
            }
            // 移动到第一位
            Tags.MoveToFirst(tagItem);

            // 样式切换
            tagItem.IsTop = true;

            // 统计置顶数量
            TopIndex += 1;
        }

        public void Expand(TagItemVM tagItem)
        {
            // 当前有标签处于展开状态
            if (IsExpanded)
            {
                // 点击的是当前展开的
                if (tagItem.IsExpanded)
                {
                    tagItem.IsExpanded = false;
                    IsExpanded = false;
                }
                // 点击的是其他未展开的
                else
                {
                    // 关闭之前展开的
                    Tags[ExpandedIndex].IsExpanded = false;
                    // 展开当前
                    tagItem.IsExpanded = true;
                    IsExpanded = true;
                    ExpandedIndex = Tags.IndexOf(tagItem);
                    VideoModelContainer.TagsVM.Tags = tagItem.Childs;
                }
            }
            // 当前没有标签处于展开状态
            else
            {
                tagItem.IsExpanded = true;
                IsExpanded = true;
                ExpandedIndex = Tags.IndexOf(tagItem);
                VideoModelContainer.TagsVM.Tags = tagItem.Childs;
            }
        }

        /// <summary>
        /// 若当前为子标签列表则关闭该列表，否则切换主页面显示状态
        /// </summary>
        public void ExpandClose()
        {
            if (IsExpanded)
            {
                Tags[ExpandedIndex].IsExpanded = false;
                IsExpanded = false;
            }
        }

        public void ClosePopup()
        {
            if (IsExpanded)
            {
                Tags[ExpandedIndex].IsExpanded = false;
                IsExpanded = false;
            }
        }

        public void Normalize()
        {
            ClosePopup();
            if (State == 0)
            {
                return;
            }
            foreach (var item in Tags)
            {
                item.IsEdit = false;
                item.Content = item.EditContent;
            }
            State = 0;
        }

        #endregion

    }
}
